package com.tech.platform.cms.authz.sso.controller;

import cn.hutool.core.collection.CollUtil;
import com.tech.platform.cms.common.constant.Constant;
import com.tech.platform.cms.common.model.CommonResult;
import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
public class SsoErrorController extends BasicErrorController {

    public SsoErrorController() {
        super(new DefaultErrorAttributes(), new ErrorProperties());
    }


    @Override
    @RequestMapping(value = "/error",produces = {MediaType.APPLICATION_JSON_VALUE})
    public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
        Map<String, Object> body = getErrorAttributes(request, isIncludeStackTrace(request, MediaType.ALL));
        HttpStatus status = getStatus(request);
        //自定义的错误信息类
        //status.value():错误代码，
        //body.get("message").toString()错误信息
        Map<String,Object> ret = CollUtil.newHashMap();
        ret.put("code",Constant.SYSTEM_ERROR);
        ret.put("message",body.get("message"));
        return new ResponseEntity<Map<String, Object>>(ret, status);
    }
}
